<template>
  <div id="root">
    <div class="todo-container">
      <div class="todo-wrap">
        <MyHeader :addTodo="addTodo"></MyHeader>
        <MyList
          :todoList="todoList"
          :checkedTodo="checkedTodo"
          :delTodo="delTodo"
        ></MyList>
        <MyFooter
          :todoList="todoList"
          :isAllCheckedTodo="isAllCheckedTodo"
          :removerTodo="removerTodo"
        ></MyFooter>
      </div>
    </div>
  </div>
</template>
<script>
import MyHeader from "./components/MyHeader";
import MyFooter from "./components/MyFooter";
import MyList from "./components/MyList";
import { nanoid } from "nanoid";
export default {
  name: "App",
  components: {
    MyHeader,
    MyList,
    MyFooter,
  },
  data() {
    return {
      todoList: [
        { id: "01", title: "吃饭", Completed: true },
        { id: "02", title: "睡觉", Completed: false },
        { id: "03", title: "打豆豆", Completed: true },
        { id: "04", title: "抽烟", Completed: false },
        { id: "05", title: "喝酒", Completed: true },
        { id: "06", title: "烫头发", Completed: false },
      ],
    };
  },
  methods: {
    addTodo(title) {
      const flag = this.todoList.find((todo) => todo.title === title);
      if (flag) return alert("您已添加过此任务");
      const obj = { id: nanoid(), title, Completed: false };
      this.todoList.unshift(obj);
    },
    checkedTodo(id) {
      this.todoList.find((todo) => {
        if (todo.id === id) {
          todo.Completed = !todo.Completed;
        }
      });
    },
    delTodo(id) {
      this.todoList = this.todoList.filter((todo) => todo.id !== id);
    },
    isAllCheckedTodo(val) {
      this.todoList.forEach((todo) => (todo.Completed = val));
    },
    removerTodo() {
      this.todoList = this.todoList.filter((todo) => !todo.Completed);
    },
  },
};
</script>

<style lang="less">
/*base*/
.btn {
  display: inline-block;
  padding: 4px 12px;
  margin-bottom: 0;
  font-size: 14px;
  line-height: 20px;
  text-align: center;
  vertical-align: middle;
  cursor: pointer;
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.2),
    0 1px 2px rgba(0, 0, 0, 0.05);
  border-radius: 4px;

  &:focus {
    outline: none;
  }
}

.btn-danger {
  color: #fff;
  background-color: #da4f49;
  border: 1px solid #bd362f;

  &:hover {
    background-color: #bd362f;
  }
}

.todo-container {
  width: 600px;
  margin: 0 auto;

  .todo-wrap {
    padding: 10px;
    border: 1px solid #ddd;
    border-radius: 5px;
  }
}
</style>
